<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\facade\Builder;
use app\common\model\Assets;
use think\App;

class Account extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Account';

    //关联模型
    protected array $hasOne = ['user', 'take', 'admin'];

    //数据排序，支持多个
    protected array $sort = ['id' => 'desc'];

    //是否主模型接管getList，1完全接管，2对象接管，3数据接管
    protected int $takeOver = 0;

    //账户流水
    public function index(): string
    {
        return $this->setTable('index');
    }

    //奖金明细
    public function bonus(): string
    {
        return $this->setTable('bonus', [['is_bonus', '=', 1]]);
    }

    //充值明细
    public function recharge(): string
    {
        return $this->setTable('recharge', [['is_admin', '=', 1]]);
    }

    public function setTable($action, $extra_map = []): string
    {
        //构建表格数据
        $table = Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'uid', 'title' => '相关会员', 'templet' => '#_userTpl', 'width' => 250])
            ->addColumn(['field' => 'type', 'title' => '账户']);
        if ($action == 'recharge') {
            $table->addColumn(['field' => 'front', 'title' => '前余额'])
                ->addColumn(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                ->addColumn(['field' => 'after', 'title' => '后余额'])
                ->addColumn(['field' => 'msg', 'title' => '备注'])
                ->addColumn(['field' => 'admin_id', 'title' => '操作人', 'templet' => '<span>{{d.admin?d.admin.admin_name:"-"}}</span>']);
        } elseif ($action == 'bonus') {
            $table->addColumn(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                ->addColumn(['field' => 'source', 'title' => '来源会员', 'templet' => '#_takeUserTpl']);
        } else {
            $table->addColumn(['field' => 'front', 'title' => '前余额'])
                ->addColumn(['field' => 'amount', 'title' => '金额', 'templet' => '<div><span class="color color_{{d.fx}}">{{d.fx}}{{d.amount}}</span></div>'])
                ->addColumn(['field' => 'after', 'title' => '后余额'])
                ->addColumn(['field' => 'source', 'title' => '来源会员', 'templet' => '#_takeUserTpl']);
        }
        $search = [
            ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['type', '账户类型', 'select', Assets::getFields(3)],
            ['source', '来源会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
            ['category', '奖金类型', 'select', getBonusTitle(1)]
        ];
        switch ($action) {
            case "index":
                unset($search[3]);
                break;
            case "recharge":
                unset($search[2]);
                unset($search[3]);
                break;
        }
        return $table->addColumn(['field' => 'create_time', 'title' => '时间', 'width' => 150])
            ->addColumn(['field' => 'intro', 'title' => '说明'])
            ->addSearch($search)
            ->addExtraMap($extra_map)
            ->addTopButtons(['export'])
            ->fetch();
    }
}